package com.modules.service.sys.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.vo.Result;
import com.modules.dao.sys.MenuDao;
import com.modules.dao.sys.RoleDao;
import com.modules.dao.sys.UserDao;
import com.modules.dto.sys.resp.MenuDtoResp;
import com.modules.entity.sys.RoleEntity;
import com.modules.entity.sys.RoleMenuEntity;
import com.modules.entity.sys.UserEntity;
import com.modules.service.sys.RoleService;


/**
 * service角色
 *
 * @author Administrator
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService{


    @Autowired
    private RoleDao dao;
    @Autowired
    private MenuDao mdao;
    @Autowired
    private UserDao udao;

    
    /**
     * 删除
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> delete(Integer rid) {
    	Integer count = udao.selectCount(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getRid, rid));
        if (count > 0) {
            return Result.fail("请删除角色对应的用户");
        }
        dao.deleteById(rid);
        return Result.success("删除成功");
    }

    /**
     * 授权菜单
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> saveRoleMenu(Integer rid, Integer[] mids) {
    	RoleMenuEntity roleMenu = new RoleMenuEntity();
		roleMenu.setRid(rid);
		mdao.deleteRoleMenu(roleMenu);
		if(mids != null && mids.length > 0) {
			List<RoleMenuEntity> list = new ArrayList<>();
			for(Integer mid : mids) {
				RoleMenuEntity rm = new RoleMenuEntity(null, rid, mid, 1);// oid 没改先设置 1 了
				list.add(rm);
			}
			mdao.insertRoleMenu(list);
		}
        return Result.success("授权成功");
    }

	@Override
	public Result<?> selectRoleMenuTree(Integer rid) {
		List<MenuDtoResp> mList = mdao.selectRoleMenu(rid);
		List<Map<String,Object>> list = new ArrayList<>();
		for(MenuDtoResp m : mList) {
			if(m.getParentId() == 0) {
				Map<String,Object> map = new HashMap<>();
				map.put("id", m.getMid());
				map.put("title", m.getMname());
				map.put("spread", true);
				List<Map<String,Object>> children = getMenuChildren(mList, m.getMid());
				if(children.size() < 1) { // 这是是没有子级的时候才判断是否选中，否则有子级的会子级全选
					map.put("checked", m.getRmstate() > 0 ? true :false);
				}
				map.put("children", children);
				list.add(map);
			}
		}
		return Result.success(list);
	}

	public List<Map<String,Object>> getMenuChildren(List<MenuDtoResp> mList, Integer pid){
		List<Map<String,Object>> list = new ArrayList<>();
		for(MenuDtoResp m : mList) {
			if(m.getParentId().equals(pid)) {
				Map<String,Object> map = new HashMap<>();
				map.put("id", m.getMid());
				map.put("title", m.getMname());
				map.put("checked", m.getRmstate() > 0 ? true :false);
				map.put("spread", true);
				map.put("children", getMenuChildren(mList, m.getMid()));
				list.add(map);
			}
		}
		return list;
	}




}
